package com.ke.rule.base;

import java.util.Stack;

/**
 * Method call stack <br>
 * In a thread, the functional unit on which the method runs may call some methods, use the
 * CallStack to record the nesting hierarchy
 * 
 * @author guyu
 *
 */
final class MethodCallStack extends Stack<IServiceMethod> {
  private static final long serialVersionUID = 1L;

  // call stack
  private static final ThreadLocal<MethodCallStack> callStack = new ThreadLocal<MethodCallStack>() {
    @Override
    protected MethodCallStack initialValue() {
      return new MethodCallStack();
    }
  };
  
  public static void enter(IServiceMethod method) {
    callStack.get().push(method);
  }
  
  public static IServiceMethod exit() {
    return callStack.get().pop();
  }

  /**
   * get last called method
   * @return
   */
  public static IServiceMethod getLastCalled() {
    return callStack.get().lastCalled;
  }
  
  private IServiceMethod lastCalled;
  private MethodCallStack() {
    super();
    this.lastCalled = null;
  }

  @Override
  public synchronized IServiceMethod pop() {
    this.lastCalled = super.pop();
    return this.lastCalled;
  }
  
}
